//
//  TravelDetailViewController.m
//  lvtu
//
//  Created by Apple on 12-6-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "TravelDetailViewController.h"
#import "EGOImageView.h"
#import "DateDetailView.h"
#import "DataPartView.h"
#import "DetailCell.h"
#import "EditTravelDetailViewController.h"
#import "NSDate+Helper.h"
#import "EventThumbViem.h"
#import "wxAppDelegate.h"

@implementation TravelDetailViewController

static NSInteger DateSort(id s1, id s2, void *context)
{
    NSDate* date1 = [NSDate dateWithDateString:(NSString*)s1];
    NSTimeInterval time1 = [date1 timeIntervalSinceReferenceDate];
    NSDate* date2 = [NSDate dateWithDateString:(NSString*)s2];
    NSTimeInterval time2 = [date2 timeIntervalSinceReferenceDate];
    
    if (time1 < time2)
        return NSOrderedDescending;
    else if (time1 > time2)
        return NSOrderedAscending;
    else
        return NSOrderedSame;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithTravelId:(NSString*) lineId travelSum:(RecommendTripInfo*) sumInfo type:(BOOL)editType
{
    self = [super init];
    if (self) {
        // Custom initialization
        tripId = [[NSString alloc] initWithString:lineId];
        sumTripInfo = sumInfo;
        [sumTripInfo retain];
        tripinfo = [[TripInfo alloc] init];
        canEdit = editType;
        [DataSource shareInstance].isEdit = YES;
        
        baseAlert = [[UIAlertView alloc] initWithTitle:@"正在加载..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        dateIndex = 2;
    }
    return self;
}

- (void)dealloc
{
    [tripId release];
    [tripinfo release];
    [sumTripInfo release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    [DataSource shareInstance].isEdit = YES;
    NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:tripId,@"c_travel_line_id",nil];
    CoreMsg* msg = [[CoreMsg alloc] init];
    msg.data = TRAVELDETAIL;
    msg.ui_delegate = self;
    
    [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=page_line_detail_new" postInfo:info userInfo:msg];
    
    [baseAlert show];
    
    UIActivityIndicatorView* aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    aiv.center = CGPointMake(baseAlert.bounds.size.width/2.0f, baseAlert.bounds.size.height-40.0f);
    [aiv startAnimating];
    [baseAlert addSubview:aiv];
    [aiv release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    [tempLabel setText:@"旅途详情"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    
    UIImage* backImage = [UIImage imageNamed:@"btn_back_normal.png"];
    UIButton* leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftbutton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
    [leftbutton setBackgroundImage:backImage forState:UIControlStateNormal];
    [leftbutton setBackgroundImage:[UIImage imageNamed:@"btn_back_press.png"] forState:UIControlStateHighlighted];
    [leftbutton addTarget:self action:@selector(backButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftbutton];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    [backButtonItem release];
    
    UIImage* editImage = [UIImage imageNamed:@"btn_editpost_normal.png"];
    rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightbutton.frame = CGRectMake(0, 0, editImage.size.width, editImage.size.height);
    [rightbutton setBackgroundImage:editImage forState:UIControlStateNormal];
    [rightbutton setBackgroundImage:[UIImage imageNamed:@"btn_editpost_press.png"] forState:UIControlStateHighlighted];
    [rightbutton addTarget:self action:@selector(editButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    if (canEdit) {
        UIBarButtonItem* editButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
        self.navigationItem.rightBarButtonItem = editButtonItem;
        [editButtonItem release];
    }
    
    UIImage* detailImage = [UIImage imageNamed:@"btn_tips.png"];
    UIButton* detailbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    detailbutton.frame = CGRectMake(260, 94, 16, 20);
    [detailbutton setBackgroundImage:detailImage forState:UIControlStateNormal];
    [detailbutton addTarget:self action:@selector(detailButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:detailbutton];
    
    UIImageView* titleBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 130)];
    UIImage* stretchImg = [[UIImage imageNamed:@"bg_titletext.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    [titleBackgroundImageView setImage:stretchImg];
    [titleBackgroundImageView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:titleBackgroundImageView];
    [titleBackgroundImageView release];
    
    UIImageView* headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 14)];
    [headImageView setImage:[UIImage imageNamed:@"bg_title_adorn.png"]];
    [headImageView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:headImageView];
    [headImageView release];
    
    cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 15, 50, 20)];
    cityLabel.font = [UIFont boldSystemFontOfSize:12];
    cityLabel.backgroundColor = [UIColor clearColor];
    cityLabel.textColor = [UIColor colorWithRed:252/255.0 green:100/255.0 blue:22/255.0 alpha:1.0];
    cityLabel.textAlignment = UITextAlignmentLeft;
    cityLabel.text = @"";
    [self.view addSubview:cityLabel];
    [cityLabel release];
    
    currentDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 15, 50, 20)];
    currentDateLabel.font = [UIFont boldSystemFontOfSize:15];
    currentDateLabel.backgroundColor = [UIColor clearColor];
    currentDateLabel.textColor = [UIColor colorWithRed:252/255.0 green:100/255.0 blue:22/255.0 alpha:1.0];
    currentDateLabel.textAlignment = UITextAlignmentLeft;
    [self.view addSubview:currentDateLabel];
    [currentDateLabel release];
    
    totleDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(135, 16, 160, 20)];
    totleDateLabel.font = [UIFont boldSystemFontOfSize:14];
    totleDateLabel.backgroundColor = [UIColor clearColor];
    totleDateLabel.textColor = [UIColor colorWithRed:122/255.0 green:143/255.0 blue:68/255.0 alpha:1];
    totleDateLabel.textAlignment = UITextAlignmentLeft;
    [self.view addSubview:totleDateLabel];
    [totleDateLabel release];
    
    tripImageView = [[EGOImageView alloc] initWithFrame:
                     CGRectMake(40, 38, 320-80, 52)];
    if (sumTripInfo.coverurl && ![sumTripInfo.coverurl isEqualToString:@""]) {
        [tripImageView setImageURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.wxlvtu.com/img.php?filename=%@",sumTripInfo.coverurl]]];
    }
    else {
        [tripImageView setImage:[UIImage imageNamed:@"bg_trip_default.png"]];
    }
    [self.view addSubview:tripImageView];
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 90, 200, 16)];
    nameLabel.text = sumTripInfo.name;
    nameLabel.backgroundColor = [UIColor clearColor]; 
    //nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:11.0f];
    nameLabel.numberOfLines = 0;
    nameLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    [self.view addSubview:nameLabel];
    [nameLabel release];
    
    editorLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 107, 50, 16)];
//    editorLabel.text = sumTripInfo.author;
    editorLabel.backgroundColor = [UIColor clearColor]; 
    editorLabel.textColor = [UIColor darkGrayColor];
    editorLabel.font = [UIFont systemFontOfSize:9.0f];
    editorLabel.numberOfLines = 0;
    editorLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    [self.view addSubview:editorLabel];
    [editorLabel release];
    
    nodataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    nodataLabel.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2-50);
    nodataLabel.text = @"无数据";
    nodataLabel.backgroundColor = [UIColor clearColor];
    nodataLabel.hidden = YES;
    [self.view addSubview:nodataLabel];
    [nodataLabel release];
    
    UIImage *eventShowViewBgImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bg_whiteframe.png" ofType:nil]];
    eventShowBgView = [[UIImageView alloc] initWithImage:[eventShowViewBgImage stretchableImageWithLeftCapWidth:30 topCapHeight:10]];
    eventShowBgView.userInteractionEnabled = YES;
    [eventShowViewBgImage release];
    [eventShowBgView setFrame:CGRectMake(30, 130, 260, 190)];
    [self.view addSubview:eventShowBgView];
    
    eventShowView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 5, 240, 180)];
    eventShowView.backgroundColor = [UIColor clearColor];
    eventShowView.contentSize = CGSizeMake(240, 200);
    eventShowView.pagingEnabled = NO;
    eventShowView.scrollEnabled = YES;
    eventShowView.clipsToBounds = YES;
    eventShowView.scrollsToTop = YES;
    eventShowView.showsVerticalScrollIndicator = YES;
    eventShowView.showsHorizontalScrollIndicator = NO;
//    eventShowView.delegate = self;
    [eventShowBgView addSubview:eventShowView];
    
    praiseButton = [UIButton buttonWithType:UIButtonTypeCustom];
	praiseButton.frame = CGRectMake(0, 322, 106, 48);
    [praiseButton setBackgroundImage:[UIImage imageNamed:@"btn_like_normal.png"] forState:UIControlStateNormal];
    [praiseButton setBackgroundImage:[UIImage imageNamed:@"btn_like_press.png"] forState:UIControlStateHighlighted];
    [praiseButton setBackgroundImage:[UIImage imageNamed:@"btn_like_press.png"] forState:UIControlStateSelected];
    [praiseButton setBackgroundImage:[UIImage imageNamed:@"btn_like_disable.png"] forState:UIControlStateDisabled];
	[praiseButton addTarget:self action:@selector(praiseButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:praiseButton];
    praiseLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 8, 60, 30)];
    praiseLabel.text = [NSString stringWithFormat:@"称赞(%@)",sumTripInfo.praisecount];
    praiseLabel.backgroundColor = [UIColor clearColor]; 
    praiseLabel.font = [UIFont systemFontOfSize:12.0f];
    [praiseButton addSubview:praiseLabel];
    if (canEdit) {
        praiseButton.enabled = NO;
    }
    
    favorbutton = [UIButton buttonWithType:UIButtonTypeCustom];
	favorbutton.frame = CGRectMake(107, 322, 106, 48);
    [favorbutton setBackgroundImage:[UIImage imageNamed:@"btn_collect_normal.png"] forState:UIControlStateNormal];
    [favorbutton setBackgroundImage:[UIImage imageNamed:@"btn_collect_press.png"] forState:UIControlStateHighlighted];
    [favorbutton setBackgroundImage:[UIImage imageNamed:@"btn_collect_press.png"] forState:UIControlStateSelected];
    [favorbutton setBackgroundImage:[UIImage imageNamed:@"btn_collect_disable.png"] forState:UIControlStateDisabled];
	[favorbutton addTarget:self action:@selector(favorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:favorbutton];
    collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 8, 60, 30)];
    collectLabel.text = [NSString stringWithFormat:@"收藏(%@)",sumTripInfo.favorcount];
    collectLabel.backgroundColor = [UIColor clearColor]; 
    collectLabel.font = [UIFont systemFontOfSize:12.0f];
    [favorbutton addSubview:collectLabel];
    if (canEdit) {
        favorbutton.enabled = NO;
    }
    
    commentbutton = [UIButton buttonWithType:UIButtonTypeCustom];
	commentbutton.frame = CGRectMake(214, 322, 106, 48);
    [commentbutton setBackgroundImage:[UIImage imageNamed:@"btn_comment_normal.png"] forState:UIControlStateNormal];
    [commentbutton setBackgroundImage:[UIImage imageNamed:@"btn_comment_press.png"] forState:UIControlStateHighlighted];
    [commentbutton setBackgroundImage:[UIImage imageNamed:@"btn_comment_press.png"] forState:UIControlStateSelected];
	[commentbutton addTarget:self action:@selector(commentButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:commentbutton];
    commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 8, 60, 30)];
    commentLabel.text = @"评论(0)";
    commentLabel.backgroundColor = [UIColor clearColor]; 
    commentLabel.font = [UIFont systemFontOfSize:12.0f];
    [commentbutton addSubview:commentLabel];
    
    nextButton = [[UIButton alloc] initWithFrame:CGRectMake(294, 155, 26, 50)];
    [nextButton setImage:[UIImage imageNamed:@"btn_next_normal.png"] forState:UIControlStateNormal];
    [nextButton setImage:[UIImage imageNamed:@"btn_next_press.png"] forState:UIControlStateHighlighted];
    [nextButton addTarget:self action:@selector(nextDayPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
    
//    NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:tripId,@"c_travel_line_id",nil];
//    CoreMsg* msg = [[CoreMsg alloc] init];
//    msg.data = TRAVELDETAIL;
//    msg.ui_delegate = self;
//    
//    [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=page_line_detail" postInfo:info userInfo:msg];
//
//    baseAlert = [[UIAlertView alloc] initWithTitle:@"正在加载..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
//    [baseAlert show];
//    
//    UIActivityIndicatorView* aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
//    aiv.center = CGPointMake(baseAlert.bounds.size.width/2.0f, baseAlert.bounds.size.height-40.0f);
//    [aiv startAnimating];
//    [baseAlert addSubview:aiv];
//    [aiv release];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [baseAlert release];
    [tripImageView release]; tripImageView = nil;
    [praiseButton release]; praiseButton = nil;
    [favorbutton release]; favorbutton = nil;
    [commentbutton release]; commentbutton = nil;
    [praiseLabel release]; praiseLabel = nil;
    [collectLabel release]; collectLabel = nil;
    [commentLabel release]; commentLabel = nil;
    [nodataLabel release]; nodataLabel = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

- (void)setCanEdit:(BOOL)type
{
    if (type) {
        UIBarButtonItem* editButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
        self.navigationItem.rightBarButtonItem = editButtonItem;
        [editButtonItem release];
    }
    else {
        self.navigationItem.rightBarButtonItem = nil;
    }
}

- (void)sendCommentSuccess
{
    commentLabel.text = [NSString stringWithFormat:@"评论(%d)",[tripinfo.commentcount intValue]+1];;
}

- (void)reloadEventShowView
{
    NSArray* eventArray = [[DataSource shareInstance] getCurrentEvent];
    if ([eventArray count] != 0) {
        eventShowBgView.hidden = NO;
        eventShowView.hidden = NO;
        
        NSInteger height = [eventArray count]/3;
        if ([eventArray count]%3 > 0) {
            height ++;
        }
        [eventShowView setContentSize:CGSizeMake(240, 90*height)];
        int index = 0;
        for (PlaceInfo* info in eventArray) {
            if ([cityLabel.text isEqual: @""]) {
                for (CityInfo* city in [DataSource shareInstance].cityArray) {
                    if ([info.cityid isEqualToString:city.cityid]) {
                        cityLabel.text = city.name;
                        break;
                    }
                }
                cityLabel.text = [NSString stringWithFormat:@"[%@]",cityLabel.text];
            }
            EventThumbViem* view = [[EventThumbViem alloc] initWithFrame:CGRectMake((76+5)*(index%3)+2, index/3*90, 76, 90) Data:info];
            [view setViewType:NO];
            [eventShowView addSubview:view];
            [view release];
            index ++;
        }
    }
    else
    {
        eventShowBgView.hidden = YES;
        eventShowView.hidden = YES;
    }
}

#pragma mark - button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 2000) {
        if (buttonIndex == 0) {
            EditTravelDetailViewController * travelDetailViewController = [[EditTravelDetailViewController alloc] initWithTravel:tripinfo];
            [self.navigationController pushViewController:travelDetailViewController animated:NO];
            [travelDetailViewController release];
        }
        else
        {
            EditTravelDetailViewController * travelDetailViewController = [[EditTravelDetailViewController alloc] initWithTravel:tripinfo];
            [self.navigationController pushViewController:travelDetailViewController animated:NO];
            [travelDetailViewController nextButtonPressed];
            [travelDetailViewController release];
        }
    }
    else if (alertView.tag == 10001)
    {
        if (buttonIndex == 1) {
            [[wxAppDelegate getAppDelegate] popAllViewController];
            [[wxAppDelegate getAppDelegate] launchFirstView];
        }
    }
}

- (void)nextDayPressed
{
    moreTripDetailViewController = [[MoreTripDetailViewController alloc] initWithTravelInfo:tripinfo recommondInfo:sumTripInfo Index:2];
    moreTripDetailViewController.delegate = self;
    [self.navigationController pushViewController:moreTripDetailViewController animated:YES];
    [moreTripDetailViewController release];
}

- (void)backButtonPress:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    //    [self removeFromParentViewController];
}

- (void)editButtonPress:(id)sender
{
    UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"编辑" message:@"请选择需要编辑的内容" delegate:self cancelButtonTitle:@"基本信息" otherButtonTitles:@"旅途事件",nil];
    Alert.tag = 2000;
    [Alert show];
    [Alert release];
    return;
}

- (void)detailButtonPress:(id)sender
{
    UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"备注" message:tripinfo.detail delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil];
    [Alert show];
    [Alert release];
    return;
}

- (void)praiseButtonPressed:(id)sender
{
    if ([wxAppDelegate getAppDelegate].isLogin == NO) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录才能称赞哟。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 10001;
        [alert show];
        [alert release];
        return;
    }
    
    if (praiseButton.isSelected == NO) {
        NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:tripId,@"c_travel_line_id",nil];
        CoreMsg* msg = [[CoreMsg alloc] init];
        msg.data = PRAISETRAVEL;
        msg.ui_delegate = self;
        
        [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=page_line_praise" postInfo:info userInfo:msg];
        
        [praiseButton setSelected:YES];
        praiseLabel.text = [NSString stringWithFormat:@"称赞(%d)",[sumTripInfo.praisecount intValue]+1];
        sumTripInfo.praisecount = [NSString stringWithFormat:@"%d",[sumTripInfo.praisecount intValue]+1];
    }
//    [praiseButton setSelected:YES];
//    praiseLabel.text = [NSString stringWithFormat:@"称赞(%d)",[sumTripInfo.praisecount intValue]+1];
}

- (void)favorButtonPressed:(id)sender
{
    if ([wxAppDelegate getAppDelegate].isLogin == NO) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录才能收藏哟。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 10001;
        [alert show];
        [alert release];
        return;
    }
    if (favorbutton.isSelected == NO) {
        NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:tripId,@"c_travel_line_id",nil];
        CoreMsg* msg = [[CoreMsg alloc] init];
        msg.data = FAVORTRAVEL;
        msg.ui_delegate = self;
        
        [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=page_line_favor" postInfo:info userInfo:msg];
        
        [favorbutton setSelected:YES];
        collectLabel.text = [NSString stringWithFormat:@"收藏(%d)",[sumTripInfo.favorcount intValue]+1];
        sumTripInfo.favorcount = [NSString stringWithFormat:@"%d",[sumTripInfo.favorcount intValue]+1];
    }
//    [favorbutton setSelected:YES];
//    collectLabel.text = [NSString stringWithFormat:@"收藏(%d)",[sumTripInfo.favorcount intValue]+1];
}

- (void)commentButtonPressed:(id)sender
{
    if ([wxAppDelegate getAppDelegate].isLogin == NO) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录才能评论哟。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = 10001;
        [alert show];
        [alert release];
        return;
    }
    MessageInfo* info = [[MessageInfo alloc] init];
    info.travellineid = tripId;
    info.name = sumTripInfo.name;
    info.creattime = sumTripInfo.creattime;
    info.commentcount = tripinfo.commentcount;
    
    CommentDetailViewController * travelDetailViewController = [[CommentDetailViewController alloc] initWithMsgInfo:info];
    travelDetailViewController.delegate = self;
    [self.navigationController pushViewController:travelDetailViewController animated:YES];
    [travelDetailViewController release];
}

- (void)touchPreDayButton
{
    dateIndex --;
    [moreTripDetailViewController.navigationController popViewControllerAnimated:NO];
    if (dateIndex == 1) {
        dateIndex = 2;
        return;
    }
    moreTripDetailViewController = [[MoreTripDetailViewController alloc] initWithTravelInfo:tripinfo recommondInfo:sumTripInfo Index:dateIndex];
    moreTripDetailViewController.delegate = self;
    
    CATransition *transition = [CATransition animation];
    transition.duration = 0.2;
    //    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:moreTripDetailViewController animated:NO];
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    [moreTripDetailViewController release];
}

- (void)touchNextDayButton
{
    dateIndex++;
    
    [moreTripDetailViewController.navigationController popViewControllerAnimated:NO];
    moreTripDetailViewController = [[MoreTripDetailViewController alloc] initWithTravelInfo:tripinfo recommondInfo:sumTripInfo Index:dateIndex];
    moreTripDetailViewController.delegate = self;
    [self.navigationController pushViewController:moreTripDetailViewController animated:YES];
    [moreTripDetailViewController release];
}


#pragma -mark DataSourceDelegate

- (void)onDataSourceSucCallback:(CoreMsg *)coreMsg withData:(id)data
{
    [baseAlert dismissWithClickedButtonIndex:0 animated:NO];
    if ([coreMsg.data isEqual: TRAVELDETAIL]) {
        nodataLabel.hidden = YES;
        tripinfo = (TripInfo*)data;
        [DataSource shareInstance].editTripInfo = tripinfo;
        [[DataSource shareInstance] setDate:tripinfo.starttime];
        nameLabel.text = tripinfo.name;
        currentDateLabel.text = [NSString stringWithFormat:@"第%d天",1];
        totleDateLabel.text = [NSString stringWithFormat:@"/ 共%d天 %@",[tripinfo.datedic count],tripinfo.starttime];
        if ([tripinfo.datedic count] == 1) {
            [nextButton setHidden:YES];
        }
        if (tripinfo.coverurl && ![tripinfo.coverurl isEqualToString:@""]) {
            [tripImageView setImageURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.wxlvtu.com/img.php?filename=%@",tripinfo.coverurl]]];
        }
        else {
            [tripImageView setImage:[UIImage imageNamed:@"bg_trip_default.png"]];
        }
        editorLabel.text = [NSString stringWithFormat:@"作者：%@",tripinfo.author];
        commentLabel.text = [NSString stringWithFormat:@"评论(%@)",tripinfo.commentcount];
        [tripinfo retain];
//        [m_tableView reloadData];
        
        NSMutableArray* removeArray = [NSMutableArray array];
        for (int i = 0; i< [eventShowView.subviews count]; i++) {
            id view = [eventShowView.subviews objectAtIndex:i];
            if ([view isKindOfClass:[EventThumbViem class]])
            {
                [removeArray addObject:view];
            }
        }
        for (int i = 0; i < [removeArray count]; i++) {
            id view = [removeArray objectAtIndex:i];
            [view removeFromSuperview];
        }
        [self reloadEventShowView];
        
        if ([wxAppDelegate getAppDelegate].isLogin) {
//            NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:tripId,@"c_travel_line_id",nil];
//            CoreMsg* msg = [[CoreMsg alloc] init];
//            msg.data = PRAISETRAVEL;
//            msg.ui_delegate = self;
//            
//            [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=page_line_praise" postInfo:info userInfo:msg];
//            
//            info = [[NSDictionary alloc] initWithObjectsAndKeys:tripId,@"c_travel_line_id",nil];
//            msg = [[CoreMsg alloc] init];
//            msg.data = FAVORTRAVEL;
//            msg.ui_delegate = self;
//            
//            [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=page_line_favor" postInfo:info userInfo:msg];
        }
    }
    else if([coreMsg.data isEqual: PRAISETRAVEL])
    {
        [praiseButton setSelected:YES];
    }
    else if([coreMsg.data isEqual: FAVORTRAVEL])
    {
        [favorbutton setSelected:YES];
    }
}

- (void)onDataSourceFailCallback:(CoreMsg *)coreMsg withData:(id)data
{
    [baseAlert dismissWithClickedButtonIndex:0 animated:NO];
    if ([coreMsg.data isEqual: TRAVELDETAIL]) {
        nodataLabel.hidden = NO;
    }
    else if([coreMsg.data isEqual: PRAISETRAVEL])
    {
        if ([data isEqual:@"exited"]) {
            [praiseButton setSelected:YES];
            praiseLabel.text = [NSString stringWithFormat:@"称赞(%d)",[sumTripInfo.praisecount intValue]-1];
            sumTripInfo.praisecount = [NSString stringWithFormat:@"%d",[sumTripInfo.praisecount intValue]-1];
        }
        else
        {
//            [praiseButton setSelected:NO];
        }
    }
    else if([coreMsg.data isEqual: FAVORTRAVEL])
    {
        if ([data isEqual:@"exited"]) {
            [favorbutton setSelected:YES];
            collectLabel.text = [NSString stringWithFormat:@"收藏(%d)",[sumTripInfo.favorcount intValue]-1];
            sumTripInfo.favorcount = [NSString stringWithFormat:@"%d",[sumTripInfo.favorcount intValue]-1];
        }
        else
        {
//            [favorbutton setSelected:YES];
        }
    }
}

@end
